/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MergerUI.java
 *
 * Created on Jan 12, 2011, 6:02:33 AM
 */
package project.jpdftools.feedback.ui;

import java.awt.Desktop;
import java.awt.Toolkit;
import java.io.File;
import java.util.logging.Level;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import project.jpdftools.animation.AnimationWorker;
import project.jpdftools.feedback.thread.FeedbackThread;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;

/**
 *
 * @author Mido
 */
public class FeedBackUI extends JDialog {

    private static boolean is_component_visible = false;

    /**
     * Creates new form MergerUI
     */
    public FeedBackUI() {
        initComponents();

        // center the component
        project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());

        // indicate that the frame is visible
        is_component_visible = true;

        LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "FeedBackUI");
    }

    public static boolean IsComponentVisible() {
        return is_component_visible;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_send = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();
        p_logger_options = new javax.swing.JPanel();
        chb_attachment = new javax.swing.JCheckBox();
        hl_open_log_file = new org.jdesktop.swingx.JXHyperlink();
        p_email = new javax.swing.JPanel();
        tf_email = new javax.swing.JTextField();
        p_feedback = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_feedback = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_TITLE"));
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                FeedBackUI.this.windowClosing(evt);
            }
        });

        bt_send.setText(LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_SEND"));
        bt_send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_sendActionPerformed(evt);
            }
        });

        bt_cancel.setText(LocalizationManager.getCurrent_resource().getString("CANCEL"));
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        chb_attachment.setSelected(true);
        chb_attachment.setText(LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_ATTACHMENT_NOTE"));

        hl_open_log_file.setText(LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_OPEN_LOG_FILE"));
        hl_open_log_file.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hl_open_log_fileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout p_logger_optionsLayout = new javax.swing.GroupLayout(p_logger_options);
        p_logger_options.setLayout(p_logger_optionsLayout);
        p_logger_optionsLayout.setHorizontalGroup(
            p_logger_optionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_logger_optionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_logger_optionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(chb_attachment)
                    .addComponent(hl_open_log_file, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(412, 412, 412))
        );
        p_logger_optionsLayout.setVerticalGroup(
            p_logger_optionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_logger_optionsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chb_attachment)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hl_open_log_file, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        p_email.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_EMAIL_LABEL"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        javax.swing.GroupLayout p_emailLayout = new javax.swing.GroupLayout(p_email);
        p_email.setLayout(p_emailLayout);
        p_emailLayout.setHorizontalGroup(
            p_emailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_emailLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tf_email, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        p_emailLayout.setVerticalGroup(
            p_emailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_emailLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tf_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        p_feedback.setBorder(javax.swing.BorderFactory.createTitledBorder(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_FEEDBACK_LABEL"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        ta_feedback.setColumns(20);
        ta_feedback.setRows(5);
        jScrollPane1.setViewportView(ta_feedback);

        javax.swing.GroupLayout p_feedbackLayout = new javax.swing.GroupLayout(p_feedback);
        p_feedback.setLayout(p_feedbackLayout);
        p_feedbackLayout.setHorizontalGroup(
            p_feedbackLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_feedbackLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        p_feedbackLayout.setVerticalGroup(
            p_feedbackLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_feedbackLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER, false)
                    .addComponent(p_logger_options, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(p_feedback, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(p_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_send)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(p_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(p_feedback, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(p_logger_options, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_send)
                    .addComponent(bt_cancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        is_component_visible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "FeedBackUI");
    }//GEN-LAST:event_bt_cancelActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_cancelActionPerformed(null);
    }//GEN-LAST:event_windowClosing

    private void bt_sendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_sendActionPerformed
        //close the frame
        bt_cancelActionPerformed(null);

        // create a thread to send feedback
        FeedbackThread thread = new FeedbackThread(new AnimationWorker(LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_SENDING_FEEDBACK")), tf_email.getText(), ta_feedback.getText(), chb_attachment.isSelected());
        // start the thread
        thread.start();
    }//GEN-LAST:event_bt_sendActionPerformed

    private void hl_open_log_fileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hl_open_log_fileActionPerformed
        File log_file = new File("EventLog.txt");
        // check to see if the file already exists
        if (log_file.exists() == true) {
            try {
                // open the file
                Desktop.getDesktop().open(log_file);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_ERROR_WHILE_OPENING_LOG_FILE"),
                        LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                LoggerManager.getLogger().log(Level.SEVERE, "Error while opening log file. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(ex.getCause()));
            }
        } else {
            // file doesn't exist
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("FEEDBACK_UI_LOG_FILE_MISSING"),
                    LocalizationManager.getCurrent_resource().getString("MISSING_TITLE"), 0);
            LoggerManager.getLogger().log(Level.SEVERE, "Log file not found.");
        }
    }//GEN-LAST:event_hl_open_log_fileActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_send;
    private javax.swing.JCheckBox chb_attachment;
    private org.jdesktop.swingx.JXHyperlink hl_open_log_file;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel p_email;
    private javax.swing.JPanel p_feedback;
    private javax.swing.JPanel p_logger_options;
    private javax.swing.JTextArea ta_feedback;
    private javax.swing.JTextField tf_email;
    // End of variables declaration//GEN-END:variables
}
